<template>
  <div class="v-container">
    <v-header title="EXEED星途限量尊享车主权益">
      <!-- <span class="back" @click="goBack()">
        <i class="el-icon-arrow-left"></i>
      </span> -->
      <span class="personal" @click="$router.push('/personal')" style="font-size:1.5em">
        <!-- <i class="el-icon-s-custom"></i> -->
        
        <img src="@/assets/images/icon_grzx.png" width="24">
      </span>
      
    </v-header>
    <div class="mt">
        <img src="@/assets/images/login_img.jpg" width="100%">
        <div id="home-category" style="margin-top:5px">
          <div class="carousel-item">
              <ul class="clear-fix">
                <router-link :to="item.path" v-for="(item, index) in iconPath1" :key="index" v-if="item.see">
                  <li class="float-left" :class="item.class">{{item.name}}</li>
                </router-link>
              </ul>
          </div>
        </div>
        <div id="home-category" style="margin-top:5px">
        <div id="login-title" style="background-color: #fff;margin-top: 10px;padding:10px">
          <h3 class="h3-style2" style="margin:0px 0px 0px 5px;">个人中心</h3>
        </div>
          <div style="margin:10px;">
            <img src="@/assets/images/2_22.png" alt="123" width="33%" @click="$router.push('/userRights')" />
            <img src="@/assets/images/2_24.png" alt="456" width="33%" @click="$router.push('/order')" />
            <img src="@/assets/images/2_26.png" alt="789" width="33%" @click="$router.push('/ticketCode')" v-if="iconPath1[5].see"/>
          </div>
        </div>
    </div>
  </div>
</template>

<script>
import vHeader from "@/components/Header.vue";
import countDown from '@/components/CountDown.vue';
import { Toast } from 'mint-ui';
import { Resource } from '@/services/resource';
import LocalStorage from '@/services/storage';
import ind_01 from '@/assets/images/ind_01.png'
import ind_02 from '@/assets/images/ind_02.png'
import ind_03 from '@/assets/images/ind_03.png'
import ind_04 from '@/assets/images/ind_04.png'
import ind_05 from '@/assets/images/ind_05.png'
import ind_06 from '@/assets/images/ind_06.png'

export default {
  name: "login",
  components: {
    vHeader,
     countDown
  },
  props: {
    title: {
      type: String,
      default: "标题"
      
    }
  },
  mounted() {
    // this.loginAuto();
    var that=this;
    let token = LocalStorage.getLocalStorage('token');
    let loginStatus = LocalStorage.getLocalStorage('loginStatus');
    let openid = LocalStorage.getLocalStorage('openid');
    let isWx=that.is_weixn();
    if (!(token && loginStatus)) {
      if(loginStatus){
     
        Resource.removeLogin
          .post({}, {matrixContent:loginStatus})
          .then(res => {
            if (res.head.resultCode == "000000") {
              let token2 = res.head.token;
              LocalStorage.setLocalStorage(
                "token",
                token2,
                59 * 60 * 1000
              );
            }
          })
      }else if(isWx){
        if(openid){
          that.loginAuto(openid);
        }else{
          let code = that.getUrlParam("code");
          if(code == null || code === ''){
            var local = window.location.href;
            Resource.getAppId
            .post({}, {})
            .then(res => {
              if (res.head.resultCode == "000000") {
                let appid = res.body.appId;
                window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+
                "&redirect_uri="+encodeURIComponent(local)+"&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
              
              }
            })
          }else{
            Resource.getOpenId
            .post({}, {"code": code})
            .then(res => {
              if (res.head.resultCode == "000000") {
                let openid = res.body.openId;
                LocalStorage.setLocalStorage(
                  "openid",
                  openid,
                  90 * 24 * 60 * 60 * 1000
                );
                that.loginAuto(openid);
              }
            })
          }
        }
      }else{
        this.$router.replace('/login');
        return;
      }
    }
  },
  created() {
    let loginStatus = LocalStorage.getLocalStorage('loginStatus');
    Resource.queryRights
      .post({}, { matrixContent: loginStatus,needToken:true})
      .then(res => {
        if (res.head.resultCode == '000000') {//已发送
          if(res.body.type == 1) {
            this.iconPath1[1].see = true
            this.iconPath1[2].see = true
            this.iconPath1[4].see = true
          } else if(res.body.type == 2) {
            this.iconPath1[0].see = true
            this.iconPath1[2].see = true
            this.iconPath1[4].see = true
          } else if(res.body.type == 3) {
            this.iconPath1[1].see = true
            this.iconPath1[4].see = true
            this.iconPath1[5].see = true
          } else if(res.body.type == 4) {
            this.iconPath1[2].see = true
            this.iconPath1[4].see = true
            this.iconPath1[5].see = true
          } else if(res.body.type == 5) {
            this.iconPath1[3].see = true
            this.iconPath1[4].see = true
            this.iconPath1[5].see = true
          }
        }else{
          Toast({
            message: res.head.resultMsg,
            position: 'middle'
          });
        }
        
        console.log(res);
      })
      .catch(err => {
        console.log(err);
      });

      Resource.userInfo
        .post({}, { matrixContent: loginStatus,needToken:true })
        .then(res => {
          if (res.head.resultCode == "000000") {
            localStorage.setItem( "userInfo", JSON.stringify(res.body));
          } else {
            Toast({
              message: res.head.resultMsg,
              position: 'middle'
            });
          }
        });
  },
  data() {
    return {
      msg: "Footera",
      showvCode: false,
      telphone: '',
      dCode: '',
      vCode: null,
      vCodeImg: '',
      iconPath1:[
        {name: '机场贵宾厅', path: '/airportVipForm', class: 'ind_01', see: false},
        {name: '高铁贵宾厅', path: '/trainVipForm', class: 'ind_02', see: false},
        {name: '机场P + R停车', path: '/airportParkingForm', class: 'ind_03', see: false},
        {name: '酒后代驾', path: '/drinkDrivingForm', class: 'ind_04', see: false},
        {name: '道路救援', path: '/roadAssistanceForm', class: 'ind_05', see: false},
        {name: '普通洗车', path: '/washCarForm', class: 'ind_06', see: false}
      ]
    };
  },
  methods:{
    // 返回上一页
    goBack(){
      this.$router.go(-1);
    },
    is_weixn(){ 
      var ua = navigator.userAgent.toLowerCase(); 
      return ua.match(/MicroMessenger/i)=='micromessenger';
    },
    getUrlParam(name){
      var reg = new RegExp('(^|&)'+name+'=([^&]*)(&|$)');
      var r = window.location.search.substr(1).match(reg);
      if(r!=null){
        return unescape(r[2]);
      }
      return null;
    },
    // 免登录
    loginAuto(openid) {
      Resource.removeLogin
        .post({}, {openId:openid})
        .then(res => {
          if (res.head.resultCode == "000000") {
            let token2 = res.head.token;
            let matrixContent = res.body.matrixContent;
            LocalStorage.setLocalStorage(
              "token",
              token2,
              59 * 60 * 1000
            );
            LocalStorage.setLocalStorage(
              "loginStatus",
              matrixContent,
              90 * 24 * 60 * 60 * 1000
            );
          }
        })
    }
  }
};
</script>

<style lang="less" scoped>
@import url("./style.less");

</style>
<style scoped>
  li >>> .mint-field-other{
    position: static;
  }
</style>